package com.mission.schedule.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.R;
import com.mission.schedule.adapter.RepeatTuiJianMoreAdapter;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.repeat607.RepeatRecommendTitle;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.NetUtil.NetWorkState;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

import static com.mission.schedule.net.VolleyRequest.context;

public class RepeatTuiJianMoreActivity extends BaseActivity implements OnClickListener {

    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;
    @ViewResId(id = R.id.more_lv)
    private ListView more_lv;

    List<RepeatRecommendTitle.PageBean.ItemsBean> list = new ArrayList<RepeatRecommendTitle.PageBean.ItemsBean>();
    RepeatTuiJianMoreAdapter adapter = null;

    Context context;
    String userid;

    ProgressUtil progressUtil = new ProgressUtil();
    SharedPrefUtil sharedPrefUtil = null;

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_repeattuijianmore);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        more_lv.setDivider(null);
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userid = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERID, "");
        list = App.getDBcApplication().getAllRepeatTypeData();
        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {
            TuiJianAsync(list.size() > 0 ? true : false);
        } else {
            if (list.size() == 0){
                Toast.makeText(context, "请检查您的网络!", Toast.LENGTH_SHORT).show();
            }
        }
        item();
    }

    @Override
    protected void setAdapter() {
        adapter = new RepeatTuiJianMoreAdapter(context, list);
        more_lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void TuiJianAsync(final boolean issetadapter) {
        if (!issetadapter){
            progressUtil.ShowProgress(context, true, true, "正在加载...");
        }
        HttpUtils httpUtils = new HttpUtils(10000);
        RequestParams params = new RequestParams();
        params.addBodyParameter("nowPage", "0");
        params.addBodyParameter("pageNum", "200");
        httpUtils.send(HttpMethod.POST, URLConstants.重复推荐分类, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                if (!TextUtils.isEmpty(responseInfo.result)) {
                    Gson gson = new Gson();
                    try {
                        RepeatRecommendTitle backbean = gson.fromJson(responseInfo.result,
                                RepeatRecommendTitle.class);
                        if (backbean.status == 0) {
                            List<RepeatRecommendTitle.PageBean.ItemsBean> lists = backbean.page.items;
                            if (lists != null && lists.size() > 0) {
                                App.getDBcApplication().deleteRepeatCommendData();
                                for (RepeatRecommendTitle.PageBean.ItemsBean bean : lists){
                                    App.getDBcApplication().saveRepeatCommendData(bean);
                                }
                                if (!issetadapter){
                                    list = backbean.page.items;
                                    setAdapter();
                                }
                            }
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
                if (!issetadapter){
                    progressUtil.dismiss();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                if (!issetadapter){
                    progressUtil.dismiss();
                }

            }
        });

        HttpUtils httpUtils1 = new HttpUtils(10000);
        httpUtils1.configCurrentHttpCacheExpiry(1000);
        httpUtils1.send(HttpMethod.GET, URLConstants.管理员, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    Gson gson = new Gson();
                    try {
                        RepeatTuiJianMoreActivity.AdminBena backbean = gson.fromJson(responseInfo.result,
                                RepeatTuiJianMoreActivity.AdminBena.class);
                        if (backbean.status == 0) {
                            String[] admins = backbean.message.split(",");
                            for (String admin : admins){
                                if (userid.equals(admin)){
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISADMIN, "1");
                                }
                            }
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_ll_back:
                Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                this.finish();
                break;

            default:
                break;
        }
    }

    private void item() {
		more_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(context, RepeatFriendsRiChengActivity.class);
				intent.putExtra("type", list.get(position).id+"");
				intent.putExtra("name", list.get(position).name);
				startActivity(intent);
			}
		});
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public class AdminBena{


        /**
         * status : 0
         * downTime : 2017-04-19T14:29:49
         * message : 260975,232380,52432,255212,255191
         * list : null
         * tDelList : null
         * delList : null
         */

        public int status;
        public String downTime;
        public String message;
        public Object list;
        public Object tDelList;
        public Object delList;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getDownTime() {
            return downTime;
        }

        public void setDownTime(String downTime) {
            this.downTime = downTime;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getList() {
            return list;
        }

        public void setList(Object list) {
            this.list = list;
        }

        public Object getTDelList() {
            return tDelList;
        }

        public void setTDelList(Object tDelList) {
            this.tDelList = tDelList;
        }

        public Object getDelList() {
            return delList;
        }

        public void setDelList(Object delList) {
            this.delList = delList;
        }
    }
}
